package com.lock.reentrantLock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @PackageName:com.lock.reentrantLock
 * @ClassName:Test04
 * @Description: lockInterruptibly 如果当前线程未被中断则获得锁，如果当前线程被中断则出现异常。
 * @Author: zhangxiaoyu
 * @date 2021/1/19 16:49
 */
public class Test04 {

    static class Ser {
        //定义锁对象
        private Lock lock = new ReentrantLock();

        public void serMethod() {
            try {

                    // lock.lock();  //获得锁定，即使调用了线程的中断方法，也没有真正的中断线程
                    lock.lockInterruptibly();   //如果线程被中断了 不会获得锁，会产生异常
                    System.out.println(Thread.currentThread().getName() + "----- begin lock");
                    //执行一段耗时的操作
                    for (int i = 0; i < Integer.MAX_VALUE; i++) {
                        new StringBuilder();
                    }

                    System.out.println(Thread.currentThread().getName() + "----- end lock");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                lock.unlock();
                System.out.println(Thread.currentThread().getName() + "*********释放锁");
            }
        }

    }

    public static void main(String[] args) throws InterruptedException {
        Ser s = new Ser();

        Runnable r = new Runnable() {
            @Override
            public void run() {
                s.serMethod();
            }
        };

        Thread t1 = new Thread(r);
        t1.start();

        Thread.sleep(50);
        Thread t2 = new Thread(r);
        t2.start();

     /*   Thread.sleep(50);
        //中断线程
        t2.interrupt();*/
    }
}
